package _021_10_4

// 第一行由字符 "qwertyuiop" 组成。
// 第二行由字符 "asdfghjkl" 组成。
// 第三行由字符 "zxcvbnm" 组成。
//

func findWords2(words []string) []string {
	keyBoard := []string{"qwertyuiop", "asdfghjkl", "zxcvbnm"}
	result := []string{}
	for i := 0; i < len(words); i++ {
		for k := 0; k < len(keyBoard); k++ {
			if check(words[i], keyBoard[k]) {
				result = append(result, words[i])
				break
			}
		}
	}
	return result
}
func check(word string, key string) bool {
	rNum := 0
	for i := 0; i < len(word); i++ {
		c := word[i]
		if c < 97 {
			c += 32
		}
		for j := 0; j < len(key); j++ {
			if c == key[j] {
				rNum++
				break
			}
		}
	}
	if rNum == len(word) {
		return true
	}
	return false
}

func findWords(words []string) []string {
	top := "qwertyuiop"

	mid := "asdfghjkl"
	bottom := "zxcvbnm"
	result := make([]string, 0)
	isSame := true
	needContinue := false
	for i := 0; i < len(words); i++ {
		needContinue = false
		for j := 0; j < len(words[i]); j++ {
			if !checkIsSame(words[i][j], top) {
				isSame = false
				if j != 0 {
					needContinue = true
				}
				break
			}
		}
		if needContinue {
			continue
		}

		if isSame {
			result = append(result, words[i])
			continue
		}
		isSame = true
		for j := 0; j < len(words[i]); j++ {
			if !checkIsSame(words[i][j], mid) {
				isSame = false
				if j != 0 {
					needContinue = true
				}
				break
			}
		}
		if isSame {
			result = append(result, words[i])
			continue
		}
		if needContinue {
			continue
		}
		isSame = true
		for j := 0; j < len(words[i]); j++ {
			if !checkIsSame(words[i][j], bottom) {
				isSame = false
				if j != 0 {
					needContinue = true
				}
				break
			}
		}

		if isSame {
			result = append(result, words[i])
			continue
		}
		isSame = true
	}
	return result
}

func checkIsSame(c byte, keyStr string) bool {
	if c < 97 {
		c += 32
	}
	for i := 0; i < len(keyStr); i++ {
		if c == keyStr[i] {
			return true
		}
	}
	return false
}
